package com.biz.primus.management.order.controller;

import com.biz.primus.management.order.feign.OrderFeignClient;
import com.biz.primus.model.order.enums.*;
import com.biz.primus.model.order.vo.response.OrderItemResponseVO;
import com.biz.primus.model.order.vo.response.OrderResponseVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.*;

/**
 * @author zzy
 * @rema 订单控制器
 * @date 2017/11/8/008
 */
@Controller
@RequestMapping("order")
public class OrderController {

    @Autowired
    private OrderFeignClient orderFeignClient;

    /**
     * 订单首页,跳转
     * @return
     */
    @GetMapping
    public ModelAndView index () {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("order/list");
        return modelAndView;
    }

    /**
     * 订单跳转详情
     * @return
     */
    @GetMapping("details")
    public ModelAndView welcome () {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("order/details");
        return modelAndView;
    }

    /**
     * 订单管理(处理页面获取数据,查询,刷新)
     * @return 前端使用datatable 必须使用aadata为key返回
     * @Exce    订单状态不能为空
     */
    @GetMapping("list")
    @ResponseBody
    public Map<String, Object> list (OrderState state, String orderCode) {
        Map<String, Object> result = new HashMap<>();
        try {
            if (state == null) throw new Exception("订单管理, 订单状态为空");
        } catch (Exception e) {
            e.printStackTrace();
        }
//        List<OrderResponseVO> orderItemVOS  = orderFeignClient.queryListByOrderStateAndOrderCode(code, state);
        List<OrderResponseVO> orderItemVOS  = getAll();
        result.put("aaData", orderItemVOS);
        return result;
    }



    private List<OrderResponseVO> getAll() {
        List<OrderResponseVO>  result = new LinkedList<>();
        for (int j = 0; j < 6; j++) {
            OrderResponseVO orderItemVO = new OrderResponseVO();
            List<OrderItemResponseVO> items = new LinkedList<>();
            for (int i = 20; i < 26; i++) {
                OrderItemResponseVO orderItemResponseVO = new OrderItemResponseVO();
                orderItemResponseVO.setProductId(Long.valueOf(i));
                orderItemResponseVO.setProductCode("产品编码" + i);
                orderItemResponseVO.setProductName("产品名称" + i);
//                orderItemResponseVO.setShippingLevel(ShippingLevel.SHIPPING_24_HOURS);//TODO 依赖问题
                orderItemResponseVO.setLogo("商品LOGO" + i);
                orderItemResponseVO.setBrandId(Long.valueOf(i));
                orderItemResponseVO.setCategoryId(Long.valueOf(i));
                orderItemResponseVO.setWeight(i);
                orderItemResponseVO.setQuantity(i);
                orderItemResponseVO.setMarketPrice(i + 5);
                orderItemResponseVO.setFinalPrice(i + 3);
                orderItemResponseVO.setMemberPrice(i);
                orderItemResponseVO.setEWalletPrice(i + 4);
                orderItemResponseVO.setFinalPrice(i + 3);
//                orderItemResponseVO.setOriginPrice(i);
                orderItemResponseVO.setSpecialOfferPrice(i - 2);
                orderItemResponseVO.setSpecialOfferQuantityLimit(i);
                orderItemResponseVO.setPromotionId(Long.valueOf(i));
                orderItemResponseVO.setPromotionItemId(Long.valueOf(i));
                orderItemResponseVO.setItemTag("商品项标签" + i);
//TODO                orderItemResponseVO.setAllocatedFreeAmount(i);
                orderItemResponseVO.setType(OrderItemType.normal);
                items.add(orderItemResponseVO);
            }
            orderItemVO.setItems(items);
            orderItemVO.setOrderName("双十一订单" + j);
            orderItemVO.setOrderCode("订单编码" + j);
            orderItemVO.setUserRemark("用户备注" + j);
            orderItemVO.setSelfRemark("商家备注" + j);
            orderItemVO.setPaymentState(PaymentState.WAIT_PAY);
            orderItemVO.setState(OrderState.WAIT_PAY);
            orderItemVO.setClient(OrderClient.wechat);
            result.add(orderItemVO);
        }
        return result;
    }
}
